package barinadroid.game;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.color.Color;
import android.graphics.Typeface;

public class GameActivity extends BaseGameActivity
{
	public final int CAMERA_WIDTH = 1920, CAMERA_HEIGHT = 1080;
	public static final int DIALOG_ALLOWDIAGONAL_ID = 0;
	private BitmapTextureAtlas mOnScreenControlTexture, mOnScreenButtonsTexture;
	public TextureRegion mOnScreenControlBaseTextureRegion, mOnScreenControlKnobTextureRegion, mOnScreenButtonATextureRegion, mOnScreenButtonBTextureRegion,
			mOnScreenButtonAPressedTextureRegion, mOnScreenButtonBPressedTextureRegion;
	public static GameActivity instance;
	public Camera camera;
	public Scene currentScene;
	public Font font;

	@Override
	public EngineOptions onCreateEngineOptions()
	{
		instance = this;
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
		engineOptions.getTouchOptions().setNeedsMultiTouch(true);
		return engineOptions;
	}

	@Override
	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception
	{
		font = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 64, Color.WHITE.getABGRPackedInt());
		font.load();
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mOnScreenControlTexture = new BitmapTextureAtlas(getTextureManager(), 430, 613, TextureOptions.NEAREST);
		this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
		this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 0, 430);
		this.mOnScreenButtonsTexture = new BitmapTextureAtlas(getTextureManager(), 550, 550, TextureOptions.NEAREST);
		this.mOnScreenButtonATextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenButtonsTexture, this, "button_a.png", 0, 0);
		this.mOnScreenButtonBTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenButtonsTexture, this, "button_b.png", 256, 0);
		this.mOnScreenButtonAPressedTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenButtonsTexture, this, "button_a_pressed.png", 0, 256);
		this.mOnScreenButtonBPressedTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenButtonsTexture, this, "button_b_pressed.png", 256, 256);
		this.mEngine.getTextureManager().loadTexture(this.mOnScreenControlTexture);
		this.mEngine.getTextureManager().loadTexture(this.mOnScreenButtonsTexture);
		this.camera.setHUD(ControlsScene.getSharedInstance().mDigitalOnScreenControl);
		this.camera.getHUD().setVisible(false);
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception
	{
		mEngine.registerUpdateHandler(new FPSLogger());
		currentScene = LoadingScene.getSharedInstance();
		pOnCreateSceneCallback.onCreateSceneFinished(currentScene);
	}

	@Override
	public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception
	{
		// TODO Auto-generated method stub
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	public static GameActivity getSharedInstance()
	{
		return instance;
	}

	public void setCurrentScene(Scene scene)
	{
		if(scene == null || getEngine() == null)
			return;
		getEngine().setScene(currentScene = scene);
		this.camera.getHUD().setVisible(currentScene instanceof GameScene);
	}

	@Override
	public void onBackPressed()
	{
		if(currentScene instanceof SettingsScene)
		{
			setCurrentScene(((SettingsScene)currentScene).getCallerScene());
			if(currentScene != null)
				return;
		}
		// if(currentScene instanceof GameScene)
		// ((GameScene)currentScene).detach();
		currentScene = null;
		GameScene.getSharedInstance().detach();
		GameScene.instance = null;
		MainMenuScene.getSharedInstance().detachSelf();
		MainMenuScene.instance = null;
		ControlsScene.instance = null;
		LoadingScene.getSharedInstance().detachSelf();
		LoadingScene.instance = null;
		SettingsScene.getSharedInstace().detachSelf();
		SettingsScene.instance = null;
		BulletPool.instance = null;
		BulletFactory.instance = null;
		super.onBackPressed();
	}
}